package com.siki.config.filterConfig.sessionConfig;

import com.alibaba.fastjson.JSON;
import com.siki.pojo.enumeration.config.GlobalServiceMsgCode;
import com.siki.utils.Deprecated.JsonResult;
import com.siki.utils.JsonInfoReturn;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Session过期处理策略
 *
 * @Author Siki
 * @Date 2020/6/5
 */
@Component
public class CustomizeSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException, ServletException {
        //获取session过期信息
        JsonInfoReturn result = JsonInfoReturn.fail(GlobalServiceMsgCode.USER_ACCOUNT_USE_BY_OTHERS);
        HttpServletResponse httpServletResponse = sessionInformationExpiredEvent.getResponse();
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(result));
    }
}
